﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ProjectSem40701.Admin
{
    public partial class RoundEdit : System.Web.UI.Page
    {
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter dpRound =
                    new AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter();
                
                DataTable dttblRound = 
                    dpRound.Get_tbl_Round_Select_All_By_RoundID(Convert.ToInt32(Request.QueryString["rid"].ToString()));
               
                this.cbbExam.DataBind();

                foreach (ListItem item in cbbExam.Items)
                {
                    if (item.Value == dttblRound.Rows[0]["ExamID"].ToString())
                    {
                        item.Selected = true;
                        lblExamID.Text = dttblRound.Rows[0]["ExamID"].ToString();
                    }
                }

                this.txtRoundName.Text = dttblRound.Rows[0]["RoundName"].ToString();
                this.txtStartTime.Text = dttblRound.Rows[0]["RoundStartTime"].ToString();
                this.txtMinMark.Text = dttblRound.Rows[0]["RoundMinMark"].ToString();
                this.txtDuration.Text = dttblRound.Rows[0]["RoundDuration"].ToString();
                this.txtDescription.Value = Server.HtmlDecode(dttblRound.Rows[0]["RoundDescription"].ToString());
                this.chkbIsFirst.Checked = Convert.ToBoolean(dttblRound.Rows[0]["RoundIsFirst"].ToString());

                foreach (ListItem items in cbbStatus.Items)
                {
                    if (items.Value == dttblRound.Rows[0]["RoundStatus"].ToString())
                    {
                        items.Selected = true;
                    }
                }
            
            }
            catch
            { 
                //bao that bai
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter dpRound =
                    new AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter();
                int ok = dpRound.tbl_Round_Update_By_RoundID(Convert.ToInt32(this.cbbExam.SelectedValue.ToString()),
                    this.txtRoundName.Text.Trim(), Convert.ToDateTime(this.txtStartTime.Text.Trim()),
                    Convert.ToDouble(this.txtMinMark.Text.Trim()), Convert.ToDouble(this.txtDuration.Text.Trim()),
                    Server.HtmlEncode(this.txtDescription.Value.Trim()), Convert.ToInt32(this.cbbStatus.SelectedValue.ToString()),
                    this.chkbIsFirst.Checked, Convert.ToInt32(Request.QueryString["rid"].ToString()));
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                { 
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //bao that bai
                Show_Dialog(FAIL);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter dpRound =
                    new AdminXSD.RoundTableAdapters.tbl_RoundTableAdapter();
                int ok = dpRound.tbl_Round_Delete_By_RoundID(Convert.ToInt32(Request.QueryString["rid"].ToString()));
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog_Del(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog_Del(FAIL);
                }
            }
            catch
            {
                //bao that bai
                Show_Dialog_Del(FAIL);
            }
        }

        protected void btnAddQuestion_Click(object sender, EventArgs e)
        {
            string rid = Request.QueryString["rid"].ToString();
            Response.Redirect("QuestionRoundAdd.aspx?rid=" + rid);
        }

        /// <summary>
        /// Close dialog here
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    //Response.Redirect("Round.aspx");
                    Response.Redirect("http://localhost:2295/Admin/ExamEdit.aspx?exid=" + lblExamID.Text);
                    break;
                case FAIL:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnUpdate_ModalPopupExtender.Enabled = false;
                    this.btnDelete_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        /// <summary>
        /// Show dialog trong trường hợp update
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Cập nhật thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Cập nhật thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, cập nhật thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnUpdate_ModalPopupExtender.Enabled = true;
            this.btnUpdate_ModalPopupExtender.Show();
        }

        /// <summary>
        /// Show dialog trong trường hợp delete
        /// </summary>
        private void Show_Dialog_Del(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Xóa thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Xóa thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Lỗi không xác định, xóa thất bại";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnDelete_ModalPopupExtender.Enabled = true;
            this.btnDelete_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

    }
}
